<!--
 * @Description: 
 * @ComposerRequire: 
 * @ComposerRemove: 
 * @Author: 阿海 <764882431@qq.com>
 * @Date: 2021-06-16 12:18:09
 * @LastEditTime: 2021-06-23 15:44:16
 * @LastEditors: 阿海
-->

<ul class="layui-nav layui-nav-tree" id="layui-side-tree" lay-filter="test">

</ul>

<script>
    var http = new Request({token_url:api.token_url,async:false,token_store_name:'rules_url'});
    var res = http.request('get',api.rules_url);
    store.set("rules",res);//layui-breadcrumb 面包屑导航
    var html = '';
    var hash = location.hash.slice(1);
    // var arr = hash.split(".");;//分割数组
    //     hash = arr[0];//第一步先删除 hash的后缀
    //如果有“/”删除第一个字符    
    // if(arr[0].indexOf("/") == 0){
    //     hash = arr[0].substr(1);
    // }
    //当没有使用hash时，使用原生模板页面时，
    if(hash == ''){
        hash = window.location.pathname
    }  

    if(res.code == 0){
        for(var i=0; i<res.data.length; i++){
            var childHtml = '';//二级菜单html
            var isOpenClass = '';
            var firstUrl = api.base_url+'#'+res.data[i].url;
            if(res.data[i]._child != undefined)
            {
                for(var j=0; j<res.data[i]._child.length; j++)
                {
                    var thirdChild  = '';//三级菜单内容
                    var childUrl = api.base_url+'#'+res.data[i]._child[j].url; //二级菜单的url
                    var isOpenChildClass = '';
                    //三级菜单
                    if(res.data[i]._child[j]._child != undefined)
                    {
                        for(var index=0; index<res.data[i]._child[j]._child.length; index++){
                           
                            if(hash == res.data[i]._child[j]._child[index].url)
                            {
                                isOpenChildClass = "layui-nav-itemed";
                                isOpenClass = "layui-nav-itemed";
                            }
                            if(res.data[i]._child[j]._child[index].is_show == 1){
                                thirdChild += '<dl class="layui-nav-child"><dd class="'+((hash == res.data[i]._child[j]._child[index].url)?'layui-this':'')+'"><a href="'+api.base_url+'#'+res.data[i]._child[j]._child[index].url+'"><i class="'+res.data[i]._child[j]._child[index].icon+'"></i>'+res.data[i]._child[j]._child[index].title+'</a></dd></dl>';
                            }
                            
                            if((res.data[i]._child[j]._child[index].is_show == 1)){
                                childUrl = 'javascript:;';
                            }
                        }

                    }
                    //二级菜单
                    if(res.data[i]._child[j].is_show ==1)
                    {
                        if(hash == res.data[i]._child[j].url)
                        {
                            isOpenClass = "layui-nav-itemed";
                        }
                        
                        childHtml += '<dl class="layui-nav-child"><dd class="'+isOpenChildClass+(((hash == res.data[i]._child[j].url) || (isOpenChildClass !='' && thirdChild == ''))?' layui-this':'')+'"><a href="'+childUrl+'"><i class="'+res.data[i]._child[j].icon+'"></i>'+res.data[i]._child[j].title+'</a>'+thirdChild+'</dd></dl>';
                        
                    }

                    if((res.data[i]._child[j].is_show == 1)){
                        firstUrl = 'javascript:;';
                    }
                }
            }
            
            //一级菜单
            if(res.data[i].is_show == 1)
            {
                html += '<li class="layui-nav-item '+isOpenClass+' '+((hash == res.data[i].url)?'layui-this':'')+'"><a class="" href="'+firstUrl+'"><i class="'+res.data[i].icon+'"></i><cite>'+res.data[i].title+'</cite></a>'+childHtml+'</li>';
            }
        }
    }
    $("#layui-side-tree").html(html);
</script>